#include "main.h"
#include "scnsInclude.h"
#include "lv_port_disp.h"
#include "lvgl.h"
#include "hwMqttHandler.h"
#include "custom.h"

static void readTest(void)
{
    ScnsLcdLayer layN=(SCNS_LCD_LAYER_AUTO);
    ScnsLcdColor*tmp=scnsMallocFrom(SCNS_MALLOC_AXI,sizeof(ScnsLcdColor)*60*60);
    {
        scnsLcdLayerAutoBegin();
        scnsLcdSetAddress(layN,(Point){0,0},(Point){0+60-1,0+60-1},0);
        scnsLcdReadColors(layN,tmp,60*60);
        scnsLcdLayerAutoEnd();
    }
    scnsLcdDisplayArrayImage(SCNS_LCD_LAYER_AUTO,(Point){160,0},tmp,SCNS_LCD_COLOR_FORMAT,NULL,60,60,0,0,0);
}

void loop_init(void)
{

}

__attribute__((noinline)) void loop(void)
{
    hwMqttSend();
    scnsMenuFresh();
#if 1
    lvgl_update();
    lv_timer_handler();
#endif
    scnsIwdgFeed();
    if(scnsMenuCanPageDisplay((uint8)-1))
    {
#if defined(SCNS_TOUCH_SCREEN_ENABLE)&&SCNS_TOUCH_SCREEN_ENABLE==1
        scnsTouchScreenDisplayButtonAll(SCNS_LCD_LAYER_AUTO,0);
#endif
        readTest();
    }
#if defined(SCNS_LCD_ENABLE)&&SCNS_LCD_ENABLE==1&&defined(SCNS_SMCTP_ENABLE)&&SCNS_SMCTP_ENABLE==1&&defined(SCNS_SMCTP_EXT_LOGGER_ENABLE)&&SCNS_SMCTP_EXT_LOGGER_ENABLE==1
    {
        const Point a=scnsLcdGetPhysicalPlu(1);
        const Point b=scnsLcdGetPhysicalPrd(1);
        static uint8 last=0;
        uint8 this=(scnsMenuCanPageDisplay(((uint8)-1))==1);
        if(this){scnsSmctpExtLoggerDisplay(SCNS_LCD_LAYER_AUTO,(Point){b.x-240,a.y},16,0,240,last!=this,6,&scnsLcdFontConsolas10,SCNS_LCD_COLOR_RED,SCNS_LCD_COLOR_BACKGROUND,0);}
        last=this;
    }
#endif
#if defined(SCNS_TM1638_ENABLE)&&SCNS_TM1638_ENABLE==1
    {
        scnsTm1638DisplayPartial(SCNS_TM1638_STB0,(const uint8[6])
            {
                scnsTimerGetMs()/1000000%10,
                scnsTimerGetMs()/100000%10,
                scnsTimerGetMs()/10000%10,
                (scnsTimerGetMs()/1000%10)|0X10,
                scnsTimerGetMs()/100%10,
                scnsTimerGetMs()/10%10,
            },0,6
        );
    }
#endif
}
